﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Models;
using System.Data;
using System.Data.SqlClient;
namespace DAL
{
    public class quanXianDal
    {
        string str = "server = LAPTOP-KE3OLTQM; database = RenLiZiYuanGuanli; uid = sa ; pwd = 123456";
        //string str = "server = SC-201709031915; database = RenLiZiYuanGuanli; uid = sa ; pwd = 123456";
        /// <summary>
        /// 权限展示
        /// </summary>
        /// <returns></returns>
        public List<Models.quanXian> showQuan()
        {
            using (SqlConnection coon = new SqlConnection(str))
            {
                if (coon.State == ConnectionState.Closed)
                {
                    coon.Open();
                }
                SqlCommand command = new SqlCommand("showQuanxian", coon);
                SqlDataReader reader = command.ExecuteReader();
                List<Models.quanXian> list = new List<quanXian>();
                while (reader.Read())
                {
                    Models.quanXian quan = new quanXian();
                    quan.qxId = Convert.ToInt32(reader["qxId"]);
                    quan.qxName = reader["qxName"].ToString();
                    quan.qxUrl = reader["qxUrl"].ToString();
                    quan.qfid = Convert.ToInt32(reader["qfid"]);
                    list.Add(quan);
                }
                try
                {
                    return list;
                }
                catch (Exception ee)
                {
                    string exect = "错误信息为：" + ee;
                    return new List<quanXian>();
                }
                finally { 
                    coon.Close();
                    coon.Dispose();
                }
            }
        }
        /// <summary>
        /// 权限查询
        /// </summary>
        /// <returns></returns>
        public List<Models.quanXian> chaQuan(int roleid)
        {
            using (SqlConnection coon = new SqlConnection(str))
            {
                if (coon.State == ConnectionState.Closed)
                {
                    coon.Open();
                }
                SqlCommand command = new SqlCommand("quanFen", coon);
                command.CommandType = CommandType.StoredProcedure;
                SqlParameter pars = new SqlParameter("@roleid",SqlDbType.Int);
                pars.Value = roleid;
                command.Parameters.Add(pars);
                SqlDataReader reader = command.ExecuteReader();
                List<Models.quanXian> list = new List<quanXian>();
                while (reader.Read())
                {
                    Models.quanXian quan = new quanXian();
                    quan.qxId = Convert.ToInt32(reader["qxId"]);
                    quan.qxName = reader["qxName"].ToString();
                    quan.qxUrl = reader["qxUrl"].ToString();
                    quan.qfid = Convert.ToInt32(reader["qfid"]);
                    list.Add(quan);
                }
                try
                {
                    return list;
                }
                catch (Exception ee)
                {
                    string exect = "错误信息为：" + ee;
                    return new List<quanXian>();
                }
                finally
                {
                    coon.Close();
                    coon.Dispose();
                }
            }
        }
        /// <summary>
        /// 批量删除权限
        /// </summary>
        /// <param name="roleid"></param>
        /// <returns></returns>
        public int delQuan(string roleid)
        {
            using (SqlConnection coon = new SqlConnection(str))
            {
                if (coon.State == ConnectionState.Closed)
                {
                    coon.Open();
                }
                string delstr = string.Format("delete from roleBased where RoleId in ({0})",roleid);
                SqlCommand command = new SqlCommand(delstr,coon);
                try
                {
                    return command.ExecuteNonQuery();
                }
                catch
                {
                    return 0;
                }
                finally {
                    coon.Close();
                    coon.Dispose();
                }
            }
        }
        /// <summary>
        /// 批量添加权限
        /// </summary>
        /// <param name="roleid"></param>
        /// <param name="qxid"></param>
        /// <returns></returns>
        public int addQuan(int roleid,string qxid)
        {
            using (SqlConnection coon = new SqlConnection(str))
            {
                if (coon.State == ConnectionState.Closed)
                {
                    coon.Open();
                }
                string[] qxids = qxid.Split(',');
                string qxidtwo = "";
                foreach (var item in qxids)
                {
                    qxidtwo += string.Format("insert into roleBased values({0},{1})",roleid,item);
                }


                SqlCommand command = new SqlCommand(qxidtwo, coon);
                try
                {
                    return command.ExecuteNonQuery();
                }
                catch
                {
                    return 0;
                }
                finally
                {
                    coon.Close();
                    coon.Dispose();
                }
            }
        }

    
    }
}
